/*
	Contact Map
*/


;(function($, window, undefined)
{
	
})(jQuery, window);

function initialize() 
{
	var block_bg = colorToHex( jQuery('.block_bg_color').css('backgroundColor') ),
		
		map_data = jQuery("#map-canvas").data(),
	
		map_canvas = document.getElementById('map-canvas'),
		pos = new google.maps.LatLng(map_data.lat,map_data.lon),
		
		map_type = map_data.type == 'hybrid' ? google.maps.MapTypeId.HYBRID : google.maps.MapTypeId.ROADMAP,
		
		map = new google.maps.Map(map_canvas, {
			center: pos,
			zoom: map_data.zoom,
			mapTypeId: map_type,
			scrollwheel: false,
			//mapTypeControl: false,
			scaleControl: false,
			zoomControl: true,
			panControl: false,
			overviewMapControl: false
		}),
		grayscale = [
			{
				featureType: "all",
				elementType: "all",
				stylers: [
					{invert_lightness: map_data.invert == 1 ? true : false},
					
					{ 
						hue: block_bg, 
						saturation: 73, 
						lightness: 59, 
						gamma: 0.5
					}
				]
			}
		],
		mapType = new google.maps.StyledMapType(grayscale, { name:"Grayscale Map" });	
		
		
	map.mapTypes.set('grayscale', mapType);
	map.setMapTypeId('grayscale');
	
	// Marker
	var marker = new google.maps.Marker({map: map, position: pos, icon: map_data.pin});
	
	// Click Enable Scroll
	google.maps.event.addListener(map, 'click', function(event)
	{
		map.setOptions({scrollwheel: true})
	});
	
	google.maps.event.addListener(map, 'dragend', function(event)
	{
		map.setOptions({scrollwheel: true})
	})
	
	// Hover Out Disable Scroll
	map_canvas.onmouseout = function(event)
	{
		map.setOptions({scrollwheel: false})
	};
	
	
	// Resize Event
	jQuery(window).bind('afterresize', function(ev)
	{
		map.panTo(pos);
	});
}

function colorToHex(color) {
	if (color.substr(0, 1) === '#') {
		return color;
	}
	var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
	
	var red = parseInt(digits[2]);
	var green = parseInt(digits[3]);
	var blue = parseInt(digits[4]);
	
	var rgb = blue | (green << 8) | (red << 16);
	return digits[1] + '#' + rgb.toString(16);
};

google.maps.event.addDomListener(window, 'load', initialize);